package JB1;

import java.util.Scanner;

public class CalSeeker {
        
        final static int week_day_count = 7;
        final static int year_day_count = 365;
        private static enum weekDays  {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
        private static enum yearMonth  { January, February, March, April, May,
                        June, July, August, September, October, November,
                        December };
        
public static void main(String[] args) {
                getConsole();
        }
        private static int getIntMonth(String month) {

                for (int i = 0; i < yearMonth.values().length; i++) {
                        if (month.equals(yearMonth.values()[i].toString())) {
                                return i;
                        }
                }
                return -1;
        }
        private static int getMonthDayCount (int month, int year){
                
                if (month == 0){
                	return 31;//January
                }
                if (month == 7){
                	return 31;//August
                }
                if (month == 1){
                        if (isYearLeap(year)){
                        	return 29;
                        }
                        return 28;
                }//February
                month = month - 2;
                if (month <= 4) {
                        if ((month % 2) == 0) {
                                return 31;
                        } else {
                                return 30;
                        }
                }else {
                        if ((month % 2) != 0) {
                                return 31;
                        } else {
                                return 30;
                        }
                }               
        }
        private static int convertToDayCount (int day,int month,int year){
                
                int dayCount = 0;
                int yearToDayCount = 0;
                int monthToDayCount = 0;
                
                for (int i = 0; i < year; i++) {
                        if (!isYearLeap(i)) {
                                yearToDayCount = yearToDayCount + year_day_count;
                        } else
                                yearToDayCount = yearToDayCount + (year_day_count + 1);
                }
                // adding month days
                for (int i = 0; i < month; i++) {
                        monthToDayCount = monthToDayCount + getMonthDayCount(i, year);
                }
                dayCount = yearToDayCount + monthToDayCount + day;
                return dayCount;
        }
        private static boolean isYearLeap (int year){
                
                if (year % 400 == 0){
                	return true;
                }
                if (year % 100 != 0){
                	if (year % 4 ==0){
                		return true;
                	}
                }
                        return false;
        }
        private static int getDayNumber(int day, int month, int year) {
                int dayNumber = 0;
                int pointDayCount = convertToDayCount(1, 0, 1900);// Setting point of count, 1 Jan 1900-Monday.
                int gotDayCount = convertToDayCount(day, month, year);
                int dayRange = gotDayCount - pointDayCount;
                int fullWeeks = dayRange / week_day_count;

                if (dayRange % week_day_count != 0) {
                        dayNumber = dayRange - (fullWeeks * week_day_count);
                }
                return dayNumber;
        }
        private static void getConsole (){
                
                int day = 0;
                String month = "";
                int year = 0;
                
                Scanner scan = new Scanner(System.in);
                System.out.println("Please enter day month year(ex. 23 January 2012):");
                // Check if numeric
                if (scan.hasNextInt()) {
                        day = scan.nextInt();
                } else {
                        System.out.println("Day should be numeric.");
                        getConsole();
                        scan.close();
                        return;
                }
                month = scan.next();
                checkMonthConsole(month);
                if (scan.hasNextInt()) {
                	year = scan.nextInt();
                } else {
                    System.out.println("Year should be numeric.");
                    getConsole();
                    scan.close();
                    return;
                }
                checkDayConsole (day,getIntMonth(month),year);
                
                System.out.print(weekDays.values()[getDayNumber(day, getIntMonth(month), year)]);
                scan.close();
        }
        private static void checkMonthConsole (String month){
        	if (getIntMonth(month) < 0) {
                System.out.println("Month was entered incorrectly.Please check possible options:");
                for (int i = 0; i < yearMonth.values().length; i++) {
                        System.out.print(yearMonth.values()[i] + " ");
                        if ((i + 1) % 4 == 0) {
                                System.out.println();
                        }
                }
                System.out.println("");
                getConsole();
                return;
        	}
        }

        private static void checkDayConsole(int day, int month, int year) {
        	if (day < 1 | day > getMonthDayCount(month, year)) {
        		System.out.println("Day number must be more than 1 and less than "+ (getMonthDayCount(month, year) + 1));
        		getConsole();
        		return;
        	}
        }
}
